package com.example.dryyzx.controller;

import com.example.dryyzx.dao.CustomerMapper;
import com.example.dryyzx.dao.NursingContentMapper;
import com.example.dryyzx.dao.NursingRecordMapper;
import com.example.dryyzx.dao.UserMapper;
import com.example.dryyzx.encapsulation.NursingRecordEncapsulation;
import com.example.dryyzx.encapsulation.NursingRecordEncapsulation2;
import com.example.dryyzx.encapsulation.NursingRecordEncapsulation3;
import com.example.dryyzx.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Objects;

@Controller
public class NursingRecordController {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private NursingRecordMapper nursingRecordMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private NursingContentMapper nursingContentMapper;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("nursingrecord_page/addRecord")
    public @ResponseBody int addNursingRecord(String name,String customer_id,String project,String date,
                                                 String uname,String user_id) throws ParseException {
        int max = 6;
        NursingRecord nursingRecord = new NursingRecord();
        Customer customer = customerMapper.selectByPrimaryKey1(Integer.valueOf(customer_id));
        if(customer == null||(!customer.getName().equals(name))){
            return -1;
        }else {
            nursingRecord.setCustomerId(Integer.parseInt(customer_id));
            NursingContent nursingContent = nursingContentMapper.selectByName(project);
            if(nursingContent == null){
                return -3;
            }
            nursingRecord.setNursingContentId(nursingContent.getId());
            nursingRecord.setNurseId(Integer.parseInt(user_id));
            User user = userMapper.selectByPrimaryKey(Integer.parseInt(user_id));
            if(user ==null || !Objects.equals(user.getName(), uname)){
                return -2;
            }
            nursingRecord.setStartTime(sdf.parse(date));
            nursingRecordMapper.insertSelective(nursingRecord);
            int totalpage;
            totalpage = (nursingRecordMapper.countRecordNumber() % max != 0) ?
                    nursingRecordMapper.countRecordNumber() / max + 1 : nursingRecordMapper.countRecordNumber() / max;

            return totalpage;
        }
    }

    //编辑护理记录
    @RequestMapping("nursingrecord_page/editRecord")
    public @ResponseBody int editNursingRecord(String customer_id,String name,String project,String date,
                                              String user_id,String uname,String id) throws ParseException {
        int max = 6;
        NursingRecord nursingRecord = nursingRecordMapper.selectByPrimaryKey(Integer.parseInt(id));
        Customer customer = customerMapper.selectByPrimaryKey1(Integer.parseInt(customer_id));

        if(customer == null||(!customer.getName().equals(name))){
            return -1;
        }else {
            nursingRecord.setCustomerId(customer.getId());
            NursingContent nursingContent = nursingContentMapper.selectByName(project);
            if(nursingContent == null){
                return -3;
            }
            nursingRecord.setNursingContentId(nursingContent.getId());
            nursingRecord.setNurseId(Integer.parseInt(user_id));
            User user = userMapper.selectByPrimaryKey(Integer.parseInt(user_id));
            if(user ==null || !Objects.equals(user.getName(), uname)){
                return -2;
            }
            nursingRecord.setStartTime(sdf.parse(date));
            nursingRecordMapper.updateByPrimaryKeySelective(nursingRecord);
            int totalpage;
            totalpage = (nursingRecordMapper.countRecordNumber() % max != 0) ?
                    nursingRecordMapper.countRecordNumber() / max + 1 : nursingRecordMapper.countRecordNumber() / max;

            return totalpage;
        }
    }

    //删除护理记录
    @RequestMapping("nursingrecord_page/delRecord")
    public @ResponseBody int delNursingRecord(String id) throws ParseException {
        int max = 6;
        NursingRecord nursingRecord = nursingRecordMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(nursingRecord == null){return -1;}
        nursingRecord.setEnable(false);
        nursingRecordMapper.updateByPrimaryKeySelective(nursingRecord);
        int totalpage;
        totalpage = (nursingRecordMapper.countRecordNumber() % max != 0) ?
                nursingRecordMapper.countRecordNumber() / max + 1 : nursingRecordMapper.countRecordNumber() / max;
        return totalpage;
    }

    //获得所有（有效）护理记录
    @RequestMapping("nursingrecord_page/getRecord")
    public @ResponseBody String getNursingRecord(String page) throws ParseException {
        int max = 6;
        int recordNumber = nursingRecordMapper.countRecordNumber();
        int offset = recordNumber % max;
        NursingRecordEncapsulation3 nursingRecordEncapsulation3 = new NursingRecordEncapsulation3();
        int totalpage;
        totalpage = (recordNumber % max != 0) ?
                recordNumber / max + 1 : recordNumber / max;
        if(totalpage == 0){
            return new JsonResult(false,"没有有效的护理记录").toString();
        }
        if(offset == 0) {
            ArrayList<NursingRecordEncapsulation> nursingRecordEncapsulations = nursingRecordMapper.selectByPage((Integer.parseInt(page)-1) * max , max);
            ArrayList<NursingRecordEncapsulation2> nursingRecordEncapsulation2 = new ArrayList<>();
            for(int i=0;i<nursingRecordEncapsulations.size();i++){
                NursingRecordEncapsulation nursingRecordEncapsulation = nursingRecordEncapsulations.get(i);
                nursingRecordEncapsulation2.add(new NursingRecordEncapsulation2(nursingRecordEncapsulation.getId(),nursingRecordEncapsulation.getName(),nursingRecordEncapsulation.getCustomerId(),nursingRecordEncapsulation.getProject(),
                        sdf.format(nursingRecordEncapsulation.getDate()),nursingRecordEncapsulation.getUname()));
            }
            nursingRecordEncapsulation3.setTotalPage(totalpage);
            nursingRecordEncapsulation3.setNursingRecordEncapsulation2s(nursingRecordEncapsulation2);
            return new JsonResult(true, nursingRecordEncapsulation3).toString();
        }else {
            if(Integer.parseInt(page) == totalpage){
                ArrayList<NursingRecordEncapsulation> nursingRecordEncapsulations = nursingRecordMapper.selectByPage((Integer.parseInt(page)-1) * max , offset);
                ArrayList<NursingRecordEncapsulation2> nursingRecordEncapsulation2 = new ArrayList<>();
                for(int i=0;i<nursingRecordEncapsulations.size();i++){
                    NursingRecordEncapsulation nursingRecordEncapsulation = nursingRecordEncapsulations.get(i);
                    nursingRecordEncapsulation2.add(new NursingRecordEncapsulation2(nursingRecordEncapsulation.getId(),nursingRecordEncapsulation.getName(),nursingRecordEncapsulation.getCustomerId(),nursingRecordEncapsulation.getProject(),
                            sdf.format(nursingRecordEncapsulation.getDate()),nursingRecordEncapsulation.getUname()));
                }
                nursingRecordEncapsulation3.setTotalPage(totalpage);
                nursingRecordEncapsulation3.setNursingRecordEncapsulation2s(nursingRecordEncapsulation2);
                return new JsonResult(true, nursingRecordEncapsulation3).toString();
            } else if (Integer.parseInt(page)<=0 || Integer.parseInt(page)>totalpage) {
                return new JsonResult(false,"页数必须大于等于0").toString();
            } else{
                ArrayList<NursingRecordEncapsulation> nursingRecordEncapsulations = nursingRecordMapper.selectByPage((Integer.parseInt(page) -1)* max , max);
                ArrayList<NursingRecordEncapsulation2> nursingRecordEncapsulation2 = new ArrayList<>();
                for(int i=0;i<nursingRecordEncapsulations.size();i++){
                    NursingRecordEncapsulation nursingRecordEncapsulation = nursingRecordEncapsulations.get(i);
                    nursingRecordEncapsulation2.add(new NursingRecordEncapsulation2(nursingRecordEncapsulation.getId(),nursingRecordEncapsulation.getName(),nursingRecordEncapsulation.getCustomerId(),nursingRecordEncapsulation.getProject(),
                            sdf.format(nursingRecordEncapsulation.getDate()),nursingRecordEncapsulation.getUname()));
                }
                nursingRecordEncapsulation3.setTotalPage(totalpage);
                nursingRecordEncapsulation3.setNursingRecordEncapsulation2s(nursingRecordEncapsulation2);
                return new JsonResult(true, nursingRecordEncapsulation3).toString();
            }
        }
    }


}
